package com.jimet.security;

import java.security.MessageDigest;

public class MD5 {

	/** Generates 32-length HEX String MD5 hash value for the given string value.
	 * @param userSSN - String to hash.
	 * @return 32 length Hex String
	 */
	public static String hash(String userSSN)
	{

	if(userSSN == null || "".equals(userSSN))
	return "";


	byte buf[] = userSSN.getBytes();

	StringBuffer hexString = new StringBuffer();

	try
	{
	MessageDigest algorithm = MessageDigest.getInstance("MD5");
	algorithm.reset();
	algorithm.update(buf);
	byte[] digest = algorithm.digest();
	for(int i=0; i< digest.length; i++)
	{

		String tempAli = Integer.toHexString(digest[i]);
		if(tempAli.indexOf("ffffff") == 0)
			tempAli = tempAli.substring(6);

	if(tempAli.length()==1)
		tempAli = "0"+tempAli;

		hexString.append(tempAli);
	}
	}
	catch(Exception ex)
	{
	ex.printStackTrace();
	return "";
	}

	return hexString.toString();
	}

	  public static String getRandomPassword( int n ) {
	    char[] pw = new char[n];
	    int c  = 'A';
	    int  r1 = 0;
	    for (int i=0; i < n; i++)
	    {
	      r1 = (int)(Math.random() * 3);
	      switch(r1) {
	        case 0: c = '0' +  (int)(Math.random() * 10); break;
	        case 1: c = 'a' +  (int)(Math.random() * 26); break;
	        case 2: c = 'A' +  (int)(Math.random() * 26); break;
	      }
	      pw[i] = (char)c;
	    }
	    return new String(pw);
	  }
}
